<?php

if (!function_exists('getControllerNameOne')) {
    /**
     * 获取当前控制器的名称
     * @return mixed
     */
    function getControllerNameOne()
    {
        list($controllerStr, $action) = explode('@', request()->route()->getActionName());
        $controllerArr = explode('\\', $controllerStr);
        $controllerName = array_pop($controllerArr);
        return $controllerName;
    }
}
if (!function_exists('getClientIp')) {
    /**
     * 获取当前请求者的IP
     * @return mixed
     */
    function getClientIp()
    {
        if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
            $ip = getenv("HTTP_CLIENT_IP");
        } elseif (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        } elseif (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
            $ip = getenv("REMOTE_ADDR");
        } elseif (isset($_SERVER['REMOTE_ADDR']) &&
            $_SERVER['REMOTE_ADDR'] &&
            strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")
        ) {
            $ip = $_SERVER['REMOTE_ADDR'];
        } elseif (defined('STDIN')) {
            $ip = "cmd";
        } else {
            $ip = "unknown";
        }

        return $ip;
    }
}
if (!function_exists('getServerIp')) {
    /**
     * 获取当前服务器的IP
     * @return mixed
     */
    function getServerIp()
    {
        $ip = getenv("REMOTE_ADDR");
        return $ip;
    }
}
if (!function_exists('cimLog')) {
    /**
     *  laravel内部保存的日志（业务需求自主存储）
     * @param $error
     * @return $this
     */
    function cimLog($info)
    {
    }
}
if (!function_exists('cimLogCenter')) {
    /**$userId不为空时上报ELK
     * @param $level
     * @param $getinfo  可以传数组和字符串
     * @param $explantion  模块名称
     * @param $userId
     */
    function cimLogCenter($level, $getinfo, $explantion, $userId = '')
    {
        $info = ['模块' => $explantion, 'mlevel' => $level];
        if (is_string($getinfo)) {
            $tmp_str = $getinfo;
            $info = [];
            $info['日志'] = $tmp_str;
        } else {
            $info['日志'] = json_encode($getinfo);
        }
         //大整数用int会有显示问题
        if (isset($info['callback_time'])) {
            $info['callback_time'] = date("Y-m-d H:i:s", $info['callback_time']);
        }
        if (isset($info['password'])) {
            unset($info['password']);   //password不应该记录
        }
        $mis = getMis();
        $info['happen_time'] = date("Y-m-d H:i:s $mis");  //精确到毫秒，用于elk的时间
        $info['类型'] = $explantion;
        if (isset($info['params']) && is_array($info['params'])) {
            $info['params'] = json_encode($info['params']);
        }
        $info['GET'] = json_encode($_GET);
        $info['时间'] = date('Y-m-d H:i:s'); //日期

        if (empty($_POST)) {
            $input = file_get_contents("php://input");
            $info['INPUT'] = $input;
        } else {
            $info['POST'] = json_encode($_POST);
        }
        $info['用户ID'] = $userId ? $userId : 1;

        $info['client_ip'] = getClientIp();
        $info['server_ip'] = '10.0.9.43';//后期再写到配置里面

        $s = str_ireplace("\t", " ", json_encode($info));
        if (env('LOG_DUBUG')) {
            $return = app('bylogcenter')->write($s);
        }
        return true;
    }
}
if (!function_exists('getMis')) {
    //获得毫秒数
    function getMis()
    {
        static $idx = 0;

        list($mis, $sec) = explode(' ', microtime());
        $mis = substr($mis, 2, 3) + ($idx++);  //毫秒数+1,严格保证时间先后，方便elk查看
        if ($mis >= 1000) {
            $mis = '999';
        } else {
            $mis = str_pad((string)$mis, 3, '0', STR_PAD_LEFT);  //严格保证3位数
        }
        return $mis;
    }
}
